package com.example.fw;

import static org.junit.Assert.fail;

import org.openqa.selenium.Proxy;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

import com.opera.core.systems.OperaDriver;

public class WebDriverHelper {

	private static WebDriver driver;
	//public static String baseUrl;
	private static StringBuffer verificationErrors = new StringBuffer();
//	private ApplicationManager manager;

	public WebDriverHelper(ApplicationManager manager) {
//		this.manager = manager;
		Proxy proxy = new Proxy();
		//proxy.setHttpProxy("192.168.202.10:3128"); 
		DesiredCapabilities capabilities = new DesiredCapabilities();
		capabilities.setCapability("proxy", proxy);
		proxy.setNoProxy("localhost");
		String browser = manager.getProperty("browser");
		if ("firefox".equals(browser))	{
			driver = new FirefoxDriver(capabilities);
		} else if ("ie".equals(browser))	{
			driver = new InternetExplorerDriver(capabilities);
		} else if ("opera".equals(browser))	{
			driver = new OperaDriver(capabilities);
		} else if ("chrome".equals(browser))	{
			driver = new ChromeDriver();
		} else {
			driver = new HtmlUnitDriver();
		}
		//baseUrl = "http://localhost:8080/addressbookv4.1.4/";
		//baseUrl = manager.getProperty("baseUrl");
//		driver.manage().timeouts().implicitlyWait(
//				Integer.parseInt(manager.getProperty("implicitWait", "30")), TimeUnit.SECONDS);
		driver.get(manager.getProperty("baseUrl"));
	}

	public void stop() {
		driver.quit();
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString)) {
			fail(verificationErrorString);
		}
	}

	public WebDriver getDriver() {
		return driver;
	}

//	public String getUrl() {
//		return baseUrl;
//	}
}